package com.version1_4.controller;

import com.alibaba.fastjson.JSONObject;
import com.util.aliyun.OCSutils;
import com.version1_4.service.TopicService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by reeveslee on 16/4/27.
 */
@Controller
public class TopicController {
    private static final Logger LOGGER = Logger.getLogger(TopicController.class);

    @Autowired
    private TopicService topicService;

    /**
     * 话题主界面 - 分页查询
     *
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/topic/main/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String topicMainPage(@PathVariable("lastId") int lastId) {
        OCSutils ocSutils = new OCSutils();
        if (ocSutils.get("topicPage" + lastId) == null) {
            ocSutils.save("topicPage" + lastId, topicService.topicMainPage(lastId), 600);
            return topicService.topicMainPage(lastId);
        } else {
            return (String) ocSutils.get("topicPage" + lastId);
        }
    }

    /**
     * 话题评论界面 - 分页查询 -最热排序
     *
     * @param topicId
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/topic/feed/{userId}/{topicId}/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String feed(HttpServletResponse response, @PathVariable("userId") int userId, @PathVariable("topicId") int topicId, @PathVariable("lastId") int lastId) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return topicService.feed(userId, topicId, lastId);
    }

    /**
     * 话题评论界面 - 分页查询 -最新排序 -只有评论内容
     *
     * @param topicId
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/topic/newFeed/{topicId}/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String newFeed(HttpServletResponse response, HttpServletRequest request, @PathVariable("topicId") int topicId, @PathVariable("lastId") int lastId) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        int userId = -1;
        if (request.getParameter("userId") != null) {
            userId = Integer.parseInt(request.getParameter("userId"));
        }
        return topicService.newFeed(topicId, lastId, userId);
    }

    /**
     * 话题标签界面 - 分页查询 -最新排序
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/topic/tag/{name}/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String tag(@PathVariable("name") String name, @PathVariable("lastId") int lastId) {
        return topicService.tag(name, lastId);
    }

    /**
     * 话题追踪信息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/follow/{userId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String follow(@PathVariable("userId") int userId) {
        return topicService.follow(userId);
    }

    /**
     * 跟踪话题
     *
     * @param userId
     * @param topicId
     * @return
     */
    @RequestMapping(value = "/follow/add/{userId}/{topicId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String followAdd(@PathVariable("userId") int userId, @PathVariable("topicId") int topicId) {
        return topicService.addFollow(userId, topicId);
    }

    /**
     * 取消跟踪话题
     *
     * @param userId
     * @param topicId
     * @return
     */
    @RequestMapping(value = "/follow/remove/{userId}/{topicId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String followRemove(@PathVariable("userId") int userId, @PathVariable("topicId") int topicId) {
        return topicService.removeFollow(userId, topicId);
    }

    /**
     * 搜索话题
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/topic/search/{key}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String search(@PathVariable("key") String key) {
        return topicService.search(key);
    }

    /**
     * 推荐话题标签
     *
     * @return
     */
    @RequestMapping(value = "/topic/recomTag", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String recomTag() {
        return topicService.recomTag();
    }

    /**
     * 分享话题
     *
     * @return
     */
    @RequestMapping(value = "/topic/share/{topicId}", method = RequestMethod.GET, produces = "text/html;charset=utf-8")
    @ResponseBody
    public ModelAndView share() {
        ModelAndView modelAndView = new ModelAndView("topic/topicShare");
        return modelAndView;
    }


}
